import axios from 'axios'
// import * as statusCode from 'src/api/statusCode'
import store from 'src/store'
import { Message } from 'element-ui'

export const Axios = axios.create({
  baseURL: process.env.BASE_API,
  timeout: 30000,
  responseType: 'json',
  // withCredentials: true,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=utf-8'
  }
})
export const sqAxios = axios.create({
  baseURL: process.env.SQ_API,
  timeout: 30000,
  responseType: 'json',
  // withCredentials: true,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=utf-8'
  }
})

Axios.interceptors.request.use(config => {
  config.method = config.method || 'post'
  const token = store.getters.token
  const isJia = store.getters.isJia
  if(token) {
    if (config.method === 'get') {
      config.params.token = token
      config.params.isJia = isJia
    } else {
      config.data.token = token
      config.data.isJia = isJia
      config.data = JSON.stringify(config.data)
    }
  }
  return config
}, err => {
  return Promise.reject(err)
})

Axios.interceptors.response.use(res => {
  const { data } = res
  let message = '请求失败'
  if(data.code && data.code !== 1) {
    message = data.message
    Message({
      message: message,
      type: 'error',
      duration: 3000
    })
  } else {
    return data
  }
  return data
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '请求错误'
        break

      case 403:
        err.message = '拒绝访问'
        break

      case 404:
        err.message = `请求地址未找到: ${err.response.config.url}`
        break

      case 408:
        err.message = '请求超时'
        break

      case 500:
        err.message = '服务器内部错误'
        break

      case 501:
        err.message = '服务未实现'
        break

      case 502:
        err.message = '网关错误'
        break

      case 503:
        err.message = '服务不可用'
        break

      case 504:
        err.message = '网关超时'
        break

      case 505:
        err.message = 'HTTP版本不受支持'
        break

      default:
        err.message = '连接错误'
    }
  }
  if (err.response) {
    Message({
      message: err.response.status + ':' + err.message,
      type: 'error',
      duration: 3000
    })
  } else if (String(err) === 'Error: Network Error') {
    Message({
      message: '无网络连接，请检查你的网络',
      type: 'error',
      duration: 3000
    })
  } 
  // else {
  //   Message({
  //     message: '请求超时',
  //     type: 'error',
  //     duration: 3000
  //   })
  // }
  return Promise.reject(err)
})

sqAxios.interceptors.request.use(config => {
  config.method = config.method || 'post'
  const token = store.getters.token
  const isJia = store.getters.isJia
  if(token) {
    if (config.method === 'get') {
      config.params.token = token
      config.params.isJia = isJia
    } else {
      config.data.token = token
      config.data.isJia = isJia
      config.data = JSON.stringify(config.data)
    }
  }
  return config
}, err => {
  return Promise.reject(err)
})

sqAxios.interceptors.response.use(res => {
  // console.log(res)
  const { data } = res
  let message = '请求失败'
  if(data.code && data.code !== 1) {
    message = data.message
    Message({
      message: message,
      type: 'error',
      duration: 3000
    })
  } else {
    return data
  }
  return data
}, err => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '请求错误'
        break

      case 403:
        err.message = '拒绝访问'
        break

      case 404:
        err.message = `请求地址未找到: ${err.response.config.url}`
        break

      case 408:
        err.message = '请求超时'
        break

      case 500:
        err.message = '服务器内部错误'
        break

      case 501:
        err.message = '服务未实现'
        break

      case 502:
        err.message = '网关错误'
        break

      case 503:
        err.message = '服务不可用'
        break

      case 504:
        err.message = '网关超时'
        break

      case 505:
        err.message = 'HTTP版本不受支持'
        break

      default:
        err.message = '连接错误'
    }
  }
  if (err.response) {
    Message({
      message: err.response.status + ':' + err.message,
      type: 'error',
      duration: 3000
    })
  } else if (String(err) === 'Error: Network Error') {
    Message({
      message: '无网络连接，请检查你的网络',
      type: 'error',
      duration: 3000
    })
  } 
  // else {
  //   Message({
  //     message: '请求超时',
  //     type: 'error',
  //     duration: 3000
  //   })
  // }
  return Promise.reject(err)
})
